package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.NoteAdapter;
import flc.ast.databinding.ActivityNoteBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class NoteActivity extends BaseAc<ActivityNoteBinding> {

    private NoteAdapter noteAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_note;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivNoteBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvNoteList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        noteAdapter = new NoteAdapter();
        noteAdapter.setShow(true);
        mDataBinding.rvNoteList.setAdapter(noteAdapter);
        noteAdapter.addChildClickViewIds(R.id.ivNoteItemGood);
        noteAdapter.setOnItemChildClickListener(this);
        noteAdapter.setOnItemClickListener(this);
        noteAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivNoteItemGood:
                List<StkResBean> listSp = AppSpUtils.getGoodList();
                RecyclerView.LayoutManager layoutManager=mDataBinding.rvNoteList.getLayoutManager();
                View itemView=layoutManager.findViewByPosition(position);
                TextView textView=itemView.findViewById(R.id.tvNoteItemGoodNum);
                ImageView imageView=itemView.findViewById(R.id.ivNoteItemGood);
                int num= Integer.parseInt(textView.getText().toString().substring(0,4));
                if (noteAdapter.getItem(position).isSelected()){
                    listSp.remove(noteAdapter.getItem(position));
                    AppSpUtils.saveGoodList(listSp);
                    noteAdapter.getItem(position).setSelected(false);
                    num--;
                }else {
                    List<StkResBean> listSave = new ArrayList<>();
                    noteAdapter.getItem(position).setSelected(true);
                    listSave.add(noteAdapter.getItem(position));
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveGoodList(listSp);
                    } else {
                        AppSpUtils.saveGoodList(listSave);
                    }
                    num++;
                }
                textView.setText(getString(R.string.good_num,num));
                if(noteAdapter.getItem(position).isSelected()){
                    imageView.setImageResource(R.drawable.adianzan);
                }else {
                    imageView.setImageResource(R.drawable.aweidianz);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
        mDataBinding.refreshNoteItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshNoteItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshNoteItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshNoteItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshNoteItem.finishRefresh(refreshTime);
            }
        });
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.NOTE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        List<StkResBean> listSp = AppSpUtils.getGoodList();
                        for (StkResBean bean : result) {
                            if (listSp != null && listSp.size() != 0) {
                                for (StkResBean stkResBean : listSp) {
                                    if (bean.equals(stkResBean)){
                                        bean.setSelected(true);
                                        break;
                                    }
                                }
                            }else {
                                break;
                            }
                        }
                        if (page == 1) {
                            noteAdapter.setList(result);
                        } else {
                            noteAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

}